#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
day 26 homework
"""
# 正则表达式练习
# 1、匹配一篇英文文章的标题 类似 The Voice Of China
"""
import re
ret = re.findall(r'(?:[A-Z][a-z]+[ ]?)+', 'The Voice Of China')
print(ret)
"""
# 2、匹配一个网址
#     类似 https://www.baidu.com http://www.cnblogs.com
"""
import re
ret = re.search(r'((https|http)://)[a-z]+\.\w+\.(com|cn|org|net)(/\w+)*', 'http://www.cnblogs.com/login')
print(ret.group())
"""
# 3、匹配年月日日期 类似 2018-12-06 2018/12/06 2018.12.06
"""
import re
ret = re.finditer(r'\d{4}[-/.]\d{2}[-/.]\d{2}', '2018-12-06 2018/12/06 2018.12.06')
for i in ret:
    print(i.group())
"""
# 4、匹配15位或者18位身份证号
"""
import re
ret = re.findall(r'^\d{17}[\dxX]$|^\d{15}$', '12345678901234567X')
print(ret)
"""
# 5、从lianjia.html中匹配出标题，户型和面积，结果如下：
# [('金台路交通部部委楼南北大三居带客厅   单位自持物业', '3室1厅', '91.22平米'), ('西山枫林 高楼层南向两居 户型方正 采光好', '2室1厅', '94.14平米')]
"""
import re
with open('lianjia.html', 'r', encoding='utf-8') as f:
    ht = f.read()
regex = r'.+?data-sl="">(?P<address>.+?)</a>.+?/</span>(?P<HouseType>.+?)<span class="divide">/</span>(?P<area>.+?)<span class="divide">'
r1 = re.findall(regex, ht, flags=re.S)
print(r1)
"""
# 6、1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))
# 从上面算式中匹配出最内层小括号以及小括号内的表达式
"""
import re
s1 = '1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
ret = re.findall('\(([^()]+)\)', s1)
print(ret)
"""
# 7、从类似9-2*5/3+7/3*99/4*2998+10*568/14的表达式中匹配出乘法或除法
"""
import re
s1 = '9-2*5/3+7/3*99/4*2998+10*568/14'
ret = re.findall(r'\d+(?:[*/]\d+)+', s1)
print(ret)
"""
# 8、通读博客，完成三级菜单
# http://www.cnblogs.com/Eva-J/articles/7205734.html
"""
import os
lst = [r'F:\Python学习\老男孩']
while lst:
    path = lst[-1]
    for name in os.listdir(path):
        file_path = os.path.join(path, name)
        if os.path.isdir(file_path):
            print(name)
    num = input('请输入文件夹名：')
    if num.upper() == 'Q':
        break
    if num.upper() == 'B':
        lst.pop()
    else:
        lst.append(os.path.join(path, num))
"""
# 9、大作业：计算器
# 1)如何匹配最内层括号内的表达式
# 2)如何匹配乘除法
# 3)考虑整数和小数
# 4)写函数，计算‘2*3’ ‘10/5’
# 5)引用4)中写好的函数，完成'2*3/4'计算
import re


def mul_div(exp):
    global s1
    exp_lst = re.split('([*/])', exp)
    if exp_lst[1] == '*':
        num = float(exp_lst[0]) * float(exp_lst[2])
        s1 = s1.replace(exp, str(num))

    if exp_lst[1] == '/':
        num = float(exp_lst[0]) / float(exp_lst[2])
        s1 = s1 = s1.replace(exp, str(num))


def add_sub(exp):
    global s1
    res = re.split('([+-])', exp)
    if res[1] == '+':
        ret = float(res[0]) + float(res[2])
        s1 = s1.replace(exp, str(ret))

    if res[1] == '-':
        ret = float(res[0]) - float(res[2])
        s1 = s1.replace(exp, str(ret))

def main(exp):
    global s1
    s1 = s1.replace('+-', '-')
    s1 = s1.replace('++', '+')
    s1 = s1.replace('--', '+')
    s1 = s1.replace('-+', '-')
    ret = re.search('\([^()]+\)', exp)
    if ret:
        ret = ret.group()
        formula = re.search('\d+(?:\.\d+)?[*/]-?\d+(?:\.\d+)?', ret)
        if formula:
            mul_div(formula.group())
        else:
            result = re.search('\d+(?:\.\d+)?[+-]\d+(?:\.\d+)?', ret)
            if result:
                add_sub(result.group())

            else:
                result = re.search('\(([^()]+)\)',ret)
                s1 = s1.replace(ret, result.group(1))

    else:
        formula = re.search('\d+(?:\.\d+)?[*/]-?\d+(?:\.\d+)?', exp)
        if formula:
            mul_div(formula.group())
        result = re.search('\d+(?:\.\d+)?[+-]\d+(?:\.\d+)?', exp)
        if result:
            add_sub(result.group())


s1 = '1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
while True:
    flag = re.findall('[-+*/]',s1)
    if not flag:
        break
    ret = main(s1)

print(s1)
